﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using WPFStudy.Extensions.Helper;

namespace WPFStudy.Extensions
{
    /// <summary>
    /// DataGridExtensions 扩展类
    /// </summary>
    public class DataGridExtensions
    {

        public static readonly DependencyProperty IsExportExcelProperty = DependencyProperty.RegisterAttached(
            "IsExportExcel", typeof(bool), typeof(DataGridExtensions), 
            new PropertyMetadata(default(bool),IsExportExcelProperty_PropertyChanged));

        private static void IsExportExcelProperty_PropertyChanged(DependencyObject element, DependencyPropertyChangedEventArgs e)
        {
            if (element is FrameworkElement fe)
            {

            }
            if (e.NewValue is bool val)
            {
                if(element is DataGridBoundColumn dgbc)
                {

                }
                //Debug.WriteLine(val);
            }
        }

        public static void SetIsExportExcel(DependencyObject element, bool value)
        {
            element.SetValue(IsExportExcelProperty, value);
            if (value)
            {
                //实测 .NET 6 无法获取 Binding属性，.NF 4 获取并设置后，再Get也会丢失
                //DataGridBoundColumn dgbc = element as DataGridBoundColumn;
                //if (dgbc != null )
                //{
                //    var bd = dgbc.Binding as BindingBase;
                //    if (GetExcelCellValueBinding(element) == null)
                //    {
                //        if(bd == null)
                //        {
                //            bd = ReflectionHelper.GetObjectPropertyValue<DataGridColumn, BindingBase>(dgbc, nameof(DataGridColumn.ClipboardContentBinding));
                //            if(bd != null)
                //            {
                //                SetExcelCellValueBinding(element, bd);
                //            }
                //        }
                //        else
                //        {
                //            SetExcelCellValueBinding(element, bd);
                //        }
                //    }
                //    if (dgbc.Header is string header)
                //    {
                //        if (string.IsNullOrEmpty(GetExcelColumnHeader(element)))
                //        {
                //            SetExcelColumnHeader(element, header);
                //        }
                //    }
                //}                
            }
        }

        public static bool GetIsExportExcel(DependencyObject element)
        {
            return (bool)element.GetValue(IsExportExcelProperty);
        }

        public static readonly DependencyProperty ExcelColumnHeaderProperty = DependencyProperty.RegisterAttached(
            "ExcelColumnHeader", typeof(string), typeof(DataGridExtensions),
            new PropertyMetadata(default(string), ExcelColumnHeader_PropertyChanged));

        private static void ExcelColumnHeader_PropertyChanged(DependencyObject element, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue is string excelColumnHeader)
            {
                if (string.IsNullOrEmpty(excelColumnHeader))
                {
                    return;
                }
                SetIsExportExcel(element, true);
            }
        }

        public static void SetExcelColumnHeader(DependencyObject element, string value)
        {
            element.SetValue(ExcelColumnHeaderProperty, value);
        }

        public static string GetExcelColumnHeader(DependencyObject element)
        {
            return (string)element.GetValue(ExcelColumnHeaderProperty);
        }

        public static readonly DependencyProperty ExcelCellValueProperty = DependencyProperty.RegisterAttached(
            "ExcelCellValue", typeof(object), typeof(DataGridExtensions),
            new PropertyMetadata(null, ExcelCellValue_PropertyChanged));

        private static void ExcelCellValue_PropertyChanged(DependencyObject element, DependencyPropertyChangedEventArgs e)
        {
            if (e.NewValue is string cellValue)
            {
                if (string.IsNullOrEmpty(cellValue))
                {
                    return;
                }
            }
        }

        public static void SetExcelCellValue(DependencyObject element, object value)
        {
            element.SetValue(ExcelCellValueProperty, value);
        }

        public static object GetExcelCellValue(DependencyObject element)
        {
            return (object)element.GetValue(ExcelCellValueProperty);
        }


        //public static readonly DependencyProperty ExcelCellValueBindingProperty = DependencyProperty.RegisterAttached(
        //    "ExcelCellValueBinding", typeof(BindingBase), typeof(DataGridExtensions),
        //    new PropertyMetadata(null, ExcelCellValueBinding_PropertyChanged));

        //private static void ExcelCellValueBinding_PropertyChanged(DependencyObject element, DependencyPropertyChangedEventArgs e)
        //{
        //    if (e.NewValue is BindingBase value)
        //    {

        //    }
        //    SetIsExportExcel(element, true);
        //}

        //public static void SetExcelCellValueBinding(DependencyObject element, BindingBase value)
        //{
        //    element.SetValue(ExcelCellValueBindingProperty, value);
        //}

        //public static BindingBase GetExcelCellValueBinding(DependencyObject element)
        //{
        //    return (BindingBase)element.GetValue(ExcelCellValueBindingProperty);
        //}
    }
}
